package com.maixin.code.service;

import java.util.ArrayList;
import java.util.List;

import com.maixin.code.dao.DAO;
import com.maixin.code.dao.RoleAuthDAO;
import com.maixin.code.entity.Authority;
import com.maixin.code.store.TreeStore;

public class AuthorityService {
	private DAO<Authority> baseDao;
	private RoleAuthDAO roleAuthDAO;
	
	public List<TreeStore> getAllAuthRole(){
		List<Authority> list = baseDao.findAll(Authority.class);
		if(null!=list&&list.size()>0){
			List<TreeStore> treeList = new ArrayList<TreeStore>();
			for (int i = 0; i < list.size(); i++) {
				Authority authority = list.get(i);
				//先获得所有的父节点的数据
				if(authority.getParentId()==null){
					TreeStore store = new TreeStore();
					store.setId(authority.getAuthoritycode());
					store.setText(authority.getAuthorityname());
					store.setLeaf(false);
					List<TreeStore> l = new ArrayList<TreeStore>();
					for (int j = 0; j < list.size(); j++) {
						Authority authorityTemp = list.get(j);
						//判断是否是子节点
						if(authority.getId().equals(authorityTemp.getParentId())){
							TreeStore storeTemp = new TreeStore();
							storeTemp.setId(authorityTemp.getAuthoritycode());
							storeTemp.setText(authorityTemp.getAuthorityname());
							storeTemp.setLeaf(true);
							l.add(storeTemp);
							store.setChildren(l);
						}
						
					}
					treeList.add(store);
				}else{
				}
			}
			return treeList;
		}
		
		return null;
	}
	
	
	public String getAllByRole(Integer roleid){
		List<Authority> list = roleAuthDAO.findAllRoles(roleid);
		String str = "";
		if(null!=list&&list.size()>0){
			for (int i = 0; i < list.size(); i++) {
				str+=list.get(i).getAuthoritycode()+",";
			}
		}
		return str;
	}
	public void delRoleAuthority(Integer roleId){
		roleAuthDAO.delRoleAuth(roleId);
	}
	
	public static void main(String[] args) {
		Integer a =32;
		Integer b = 32;
		if(a == b){
			System.err.println("===");
		}
	}
	
	public DAO<Authority> getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(DAO<Authority> baseDao) {
		this.baseDao = baseDao;
	}

	public RoleAuthDAO getRoleAuthDAO() {
		return roleAuthDAO;
	}

	public void setRoleAuthDAO(RoleAuthDAO roleAuthDAO) {
		this.roleAuthDAO = roleAuthDAO;
	}
}
